<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Manage extends Common
{
    public function userList()
    {
	    $list = Db::name('menu')->paginate(2);
	    $this->assign('list', $list);

    	$this->assign('title', '列表示例');
	    return $this->fetch();
    }

    public function memberList()
    {
    	if(Request()->isPost()){
		    echo "<pre>";
		    print_r(Request()->param());
		    exit;
	    }
    	$this->assign('title', '表单示例');
    	return $this->fetch();
    }

    public function bannerList()
    {
    	$this->assign('title', '轮播图');
	    return $this->fetch();
    }

    public function chart()
    {
    	$this->assign('title', '统计图');
	    return $this->fetch();
    }
    public function product_publicity(){
        if($_POST){
            $res=db('product_publicity')->where(['id'=>1])->update(['video'=>$_POST['video']]);
            if($res){
                $this->success("成功",url("Manage/product_publicity"));
            }else{
                $this->error("失败",url("Manage/product_publicity"));
            }
        }
        $video=db('product_publicity')->where(['id'=>1])->value('video');
        $this->assign('video',$video);
        return $this->fetch();
    }
    public function uploadImg() {
        if (empty($_FILES) ){
            echo json_encode(['code'=>1,'msg'=>'图片不能为空']);exit;
        }else{
            $file = $_FILES['image'];
            if (!is_dir("Uploads/" . date("Y-m-d"))) {
                mkdir("Uploads/" . date("Y-m-d"));
            }
            if (is_uploaded_file($file['tmp_name'])) {
                $str = stristr($file['name'], '.');
                $str=substr($str,1);
                $avatar_path = "Uploads/" . date("Y-m-d") . "/" . time() . rand(1000,9999) . '.' . $str;

                if(copy($file['tmp_name'],$avatar_path)){

                    $game_path = str_replace('Uploads/','game/headimage/',$avatar_path);
                    copy($file['tmp_name'],$game_path);
                    $cover['path'] = "/".$avatar_path;
                    $cover['status'] = 1;
                    $cover['create_time'] = time();
                    $re = db('picture')->insertGetId($cover);
                    // print_r($re);die;
                    if($re){
                        // $img=get_host() . get_cover($re, "path");
                        $hh['imgurl']="http://" . $_SERVER['HTTP_HOST'] .$cover['path'];
                        $hh['imgid']=$re;
                        $hh['name']=$file['name'];
                        echo json_encode(['code'=>0,'msg'=>'成功','info'=>$hh]);exit;
                    }else{
                        echo json_encode(['code'=>1,'msg'=>'失败']);exit;
                    }
                }else {
                    echo json_encode(['code'=>1,'msg'=>'失败']);exit;
                }
            }
        }
    }
}
